package FlyGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class FlyPanel extends JPanel {

    //水果
    int fastscore;
    int fastscoretwo;
    int FruitNum;
    boolean fastgo;
    boolean isFastgo;


    boolean isStart;
    int score;
    int num;
    Graphics g;
    Repaint repaint;
    boolean live;
    ActionMove actionMove = new ActionMove();
    BackDown backDown;
    Timer timer = new Timer(50, actionMove);
    MyBird myBird;
    MyPipe[] myPipe = new MyPipe[100];
    MyCloud[] myClouds = new MyCloud[150];
    MyFruit[] myFruits=new MyFruit[50];
    OverFrame overFrame;
    Word word;
    DbUtil dbUtil;
    Object[] params;

    public FlyPanel() {
        myBird = new MyBird();
        repaint = new Repaint();
        live = true;
        init();
        this.setFocusable(true);
        KeyListen keyListen = new KeyListen();
        this.addKeyListener(keyListen);
        timer.start();
        repaint.start();
        backDown = new BackDown();
        word = new Word();
        overFrame = new OverFrame();
        dbUtil = new DbUtil();
    }

    public void init() {
        fastgo=true;
        isFastgo=false;
        FruitNum=0;
        fastscore=0;
        fastscoretwo=0;
        num = 0;
        isStart = false;
        score = 0;
        myBird.speed = 3;
        for (int i = 0; i < 100; i++) {
            myPipe[i] = new MyPipe();
            myPipe[i].downPipex = myPipe[i].downPipex + i * 150;
            myPipe[i].upPipex = myPipe[i].upPipex + i * 150;
        }
        for (int i = 0; i < 150; i++) {
            myClouds[i] = new MyCloud();
            myClouds[i].cloudx = myClouds[i].cloudx + i * 100;
        }
        for (int i=0;i<50;i++)
        {
            myFruits[i]=new MyFruit();
            myFruits[i].fruitx=myFruits[i].fruitx+i*500;
        }
        myBird.Birdy = 200;
    }

    @Override
    protected void paintComponent(Graphics g) {
        this.g = g;
        super.paintComponent(g);
        this.setBackground(Color.blue);
        //背景图片
        FlyPicture.BackPicture.paintIcon(this, g, 0, 0);

        //        绘制柱子
        for (int i = 0; i < 100; i++) {
            if (myPipe[i].upPipex > 0 && myPipe[i].upPipex < 600) {

                myPipe[i].paintPipe(g, this);
            }

        }
//绘制云
        for (int i = 0; i < 150; i++) {
            if (!(myClouds[i].cloudx <= 0 || myClouds[i].cloudx >= 600)) {
                myClouds[i].paintCloud(g, this);
            }
        }

        //        绘制底部图片
        backDown.paintBackDown(g, this);

        //        开始的提示
        if (!isStart && live) {

            word.setColor(Color.white);
            word.setContent("按下空格开始游戏！");
            word.setSize(40);
            word.setLocateX(120);
            word.setLocateY(100);

            word.paintWord(g);
        }
        if (!isStart && !live) {

            word.setColor(Color.white);
            word.setContent("按下空格开始游戏！");
            word.setSize(40);
            word.setLocateX(120);
            word.setLocateY(100);

            word.paintWord(g);
        }

        //绘制分数
        g.setColor(Color.red);
        g.setFont(new Font("微软雅黑", Font.BOLD, 15));
        g.drawString("分数： " + score, 500, 20);


        if (myBird.Birdy < 0) {
            myBird.Birdy = 0;
        }

        //绘制小鸟
        myBird.paintBird(g, this);
        //绘制水果
        for (int i=0;i<50;i++)
        {
            if(!(myFruits[i].fruitx<0||myFruits[i].fruitx>600))
            {
                myFruits[i].paintFruit(g,this);
            }
        }

    }

    public void reStart() {
        this.init();
    }

    //键盘监听器
    class KeyListen implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {

        }

        @Override
        public void keyPressed(KeyEvent e) {

            if (!live) {
                reStart();
            }
            if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                isStart = !isStart;
                live = true;
            }
            if (isStart) {
                if (e.getKeyCode() == KeyEvent.VK_UP) {
                    myBird.status = 1;
                    myBird.Birdy = myBird.Birdy - 10;
                    System.out.println(myPipe[0].speed);
                }
                if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                    myBird.status = 0;
                    myBird.Birdy = myBird.Birdy + 10;
                }
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (isStart) {
                myBird.status = 0;
            }
        }
    }

    //图片后移
    class ActionMove implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            if (isStart) {
                myBird.Birdy = myBird.Birdy + myBird.speed;//鸟下坠
                //柱子移动
                for (int i = 0; i < 100; i++) {
                    myPipe[i].upPipex = myPipe[i].upPipex - myPipe[i].speed;
                    myPipe[i].downPipex = myPipe[i].downPipex - myPipe[i].speed;
                }
                //云移动
                for (int i = 0; i < 150; i++) {
                    myClouds[i].cloudx = myClouds[i].cloudx - 4;
                }
                //水果移动
                for (int i=0;i<50;i++)
                {
                    myFruits[i].fruitx=myFruits[i].fruitx-myPipe[i].speed;
                }
            }
            //计算分数
            if (isStart)
            {
                if (getI()>=0)
                {
                    if (myBird.Birdx>=myPipe[getI()].upPipex+myPipe[getI()].upPipew/2&&myBird.Birdx<=myPipe[getI()].upPipex+myPipe[getI()].upPipew)
                    {
                        if(isFastgo)
                        {
                            fastscoretwo=fastscoretwo+30;
                            isFastgo=false;
                        }
                        score=(getI()+1)*10+fastscoretwo;
                        fastscore=score+30;


                    }
                }
                if (getFruitNum()>=0)
                {
                    if (myBird.rect.intersects(myFruits[getFruitNum()].fruitrect))
                    {
                        fastgo=false;
                        //柱子移动
                        while (score<=fastscore)
                        {
                            for (int i = 0; i < 100; i++)
                            {
                                myPipe[i].upPipex = myPipe[i].upPipex - myPipe[i].speed;
                                myPipe[i].downPipex = myPipe[i].downPipex - myPipe[i].speed;

                            }
                            for (int i = 0; i < 50; i++)
                            {
                                myFruits[i].fruitx=myFruits[i].fruitx-myPipe[i].speed;
                            }
                            score=score+10;
                        }
                    isFastgo=true;
                    }
                    fastgo=true;
                }

            }
            //-------------------失败判定
            if (isStart) {
                if (myBird.Birdy > myBird.deadline) {
                    isStart = !isStart;
                    overFrame.setVisible(true);
                    //存入分数
                    params = new Object[]{score};
                    dbUtil.doUpdate("insert into 2_com.score(score) value(?)",params);

                    live = false;
                }
                if (getI() != -1) {
                    if ((myBird.rect.intersects(myPipe[getI()].uprect)) || (myBird.rect.intersects(myPipe[getI()].downrect))) {
                        isStart = !isStart;
                        overFrame.setVisible(true);
                        //存入分数
                        overFrame.overPanel.score = score;params = new Object[]{score};
                        dbUtil.doUpdate("insert into 2_com.score(score) value(?)",params);

                        live = false;
                    }
                }
            }
        }
    }

    public int getFruitNum(){
        for (int i = 0; i < 50; i++) {
            if ((myBird.Birdx + myBird.Birdw) >= myFruits[i].fruitx && myBird.Birdx <= (myFruits[i].fruitx+myFruits[i].fruitw))
            {
                FruitNum = i;
                return FruitNum;
            }
        }
        return -1;
    }

    public int getI() {
        for (int i = 0; i < 100; i++) {
            if ((myBird.Birdx + myBird.Birdw) >= myPipe[i].upPipex && myBird.Birdx <= (myPipe[i].upPipex + myPipe[i].upPipew)) {
                num = i;

                return num;
            }
        }
        return -1;
    }

    //重新绘制线程
    class Repaint extends Thread {
        boolean flag = true;

        @Override
        public void run() {
            while (flag) {
                repaint();
            }
        }
    }


}